<?php
/**
 * customer.php
 * @author ranuandra
 *
 * 10 Agu 2011 14:29:48
 *
 * 
 */

class Model_Customer extends ORM
{
			protected $_has_many = array(
									'polreg' 	=> array('model' => 'polreg'),
								);
			protected $_belongs_to = array(
									'area'		=> array('model' => 'area')
								);
								
			public function cek_data($nama, $alamat)
			{
				return DB::select('id')
					->from($this->_table_name)
					->where('nama', '=', $nama)
					->where('alamat', '=', $alamat)
					->execute($this->_db);				
			}
			
			public function get_datacustomer($post)
			{
				$sql = DB::select('polregs.customer_id','customers.nama', 'customers.alamat', 'areas.area')->from('customers')->join('areas', 'inner')->on('areas.id', '=', 'customers.area_id')
						->join('polregs', 'inner')->on('polregs.customer_id', '=', 'customers.id')
						->where('customers.nama', 'LIKE','%'.$post['nama'].'%');
					if(isset($post['area_id']))
					{
						if($post['area_id']!="")
						{
							if(count($post['area_id']) > 0 && is_array($post['area_id']))
							{
								$sql = $sql->where_open();
								$areas = $post['area_id'];
								//print_r($areas);
								foreach($areas as $area)
								{
									$sql= $sql->or_where('customers.area_id','=',$area);
								}
								$sql = $sql->where_close();
							}
							else 
							{
								$sql = $sql->where('customers.area_id','=',$post['area_id']);
							}
						}
					}

					if(isset($post['drthn']) && isset($post['kethn']))
					{
						if($post['drthn']!="" && $post['kethn']!="" )
						{
							$sql = $sql->and_where_open();
							$sql = $sql->and_where('polregs.tgl_daftar','BETWEEN',array($post['drthn'], $post['kethn']));
							$sql = $sql->and_where_close();
						}
					}
					$sql = $sql->group_by('polregs.customer_id,customers.nama, customers.alamat, areas.area, customers.id');
					$sql = $sql->order_by('customers.nama, customers.alamat');
					
					return $sql->execute();
			}
}